package com.citywithincity.utils;



import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;



/**
 * 集合的方法
 * @author Randy
 *
 */
public class CollectionUtil {
	
	
	/**
	 * 
	 * @param source
	 * @param splitter
	 * @return
	 */
	public static <T> List<String> explode(String source,String splitter){
		String[] values = source.split(splitter);
		List<String> list = new ArrayList<String>();
		for (String string : values) {
			list.add(string);
				
		}
		return list;
	}
	public static <T> List<String> explode(String source){
		return explode(source,",");
	}
	
	
	/**
	 * 整合
	 * 
	 */
	public static <T> String join(StringBuilder sb,Collection<T> list,String splitter){
		int count = list.size();
		int i=0;

		for (T t : list) {
			sb.append(t);
			if(i++<count-1){
				sb.append(splitter);
			}
		}
		return sb.toString();
	}
	
	
	public static <T> String join(Collection<T> list,String splitter){
		StringBuilder sb = new StringBuilder();
		return join(sb,list,splitter);
	}
	
	/**
	 * 是否包含
	 * @param arr
	 * @param element
	 * @return
	 */
	public static <T> boolean contains(T[] arr,Object element){
		for (T t : arr) {
			if(t==element){
				return true;
			}
		}
		return false;
	}
	public static <T> void addAll(Collection<T> collection, T[] array) {
		if(array==null)return;
		for (T t : array) {
			collection.add(t);
		}
		
	}
	
	public static <T> List<T> visit(List<T> list,IVisitor<T> visitor) {
		List<T> result = new ArrayList<T>();
		for (T t : list) {
			if(visitor.accept(t)){
				result.add(t);
			}
		}
		return result;
	}
	public static <T> Set<T> diff(Collection<T> src, Collection<T> dest) {
		
		Set<T> result = new HashSet<T>();
		result.addAll(src);
		result.removeAll(dest);
		
		return result;
		
		
	}
	
	
}
